package com.example.hanchen.yongkanggy.home.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.MimeTypeMap;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.example.hanchen.yongkanggy.R;
import com.example.hanchen.yongkanggy.entity.ImageAddEntity;
import com.example.hanchen.yongkanggy.http.HttpUrlManager;
import com.example.hanchen.yongkanggy.utils.Page;

import java.util.ArrayList;
import java.util.List;

public class AddImageAdapter extends RecyclerView.Adapter<AddImageAdapter.TabViewHolder> {

    private Context mContext;
    private List<ImageAddEntity> tabData = new ArrayList<>();
    private OnItemClickListener listener;

    public AddImageAdapter(Context context, OnItemClickListener listener) {
        this.mContext = context;
        this.listener = listener;
    }

    public void setTabData(List<ImageAddEntity> tabData) {
        this.tabData = tabData;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public TabViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.adapter_add_image, parent, false);
        return new TabViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull TabViewHolder holder, @SuppressLint("RecyclerView") int position) {
        //6张图片 或者一个 视频
        if (tabData.get(position).getState() == 1) {
            holder.rl_look.setVisibility(View.GONE);
            if (tabData.size() > 6) {
                holder.rl_add.setVisibility(View.GONE);
            } else if (tabData.size() <= 6) {
                holder.rl_add.setVisibility(View.VISIBLE);
            }

        } else if (tabData.get(position).getState() == 2 && !TextUtils.isEmpty(tabData.get(position).getImage())) {
            holder.rl_look.setVisibility(View.VISIBLE);
            holder.rl_add.setVisibility(View.GONE);
            Log.i("JFOEIFJEIF", "   " + HttpUrlManager.huaweiObs + tabData.get(position).getImage());
            Page.Image(mContext, tabData.get(position).getImage(), holder.ivNumber);
        } else {
            holder.rl_look.setVisibility(View.GONE);
            holder.rl_add.setVisibility(View.GONE);
        }

        String fileExtension = MimeTypeMap.getFileExtensionFromUrl(tabData.get(position).getImage());
        String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension);
        if (mimeType != null) {
            if (mimeType.startsWith("image/")) {
                // 是图片
                // 压缩图片
                Log.i("IMAGEPPPPPP", "图片" + tabData.get(position).getImage());
                holder.iv_video.setVisibility(View.GONE);
            } else if (mimeType.startsWith("video/")) {
                // 是视频
                Log.i("IMAGEPPPPPP", "视频" + tabData.get(position).getImage());
                holder.iv_video.setVisibility(View.VISIBLE);
            }
        }

        holder.rl_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tabData.get(position).getState() != 2) {
                    listener.onItemClick(position);
                }
            }
        });

        holder.iv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onItemClick2(position);
            }
        });

        /*查看大图*/
        holder.ivNumber.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                mContext.startActivity(new Intent(mContext, ImageViewActivity.class).putExtra("pathImage", tabData.get(position).getImage()));
            }
        });
    }

    @Override
    public int getItemCount() {
        return tabData.size() == 0 ? 0 : tabData.size();
    }

    class TabViewHolder extends RecyclerView.ViewHolder {
        ImageView ivNumber, iv_close, iv_video;
        RelativeLayout rl_add,rl_look;

        public TabViewHolder(@NonNull View itemView) {
            super(itemView);
            ivNumber = itemView.findViewById(R.id.ivNumber);
            iv_close = itemView.findViewById(R.id.iv_close);
            iv_video = itemView.findViewById(R.id.iv_video);
            rl_add = itemView.findViewById(R.id.rl_add);
            rl_look = itemView.findViewById(R.id.rl_look);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);

        void onItemClick2(int position);
    }
}


